<template>
  <!-- 增加修改 -->
  <a-drawer width="50%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules" :layout="'horizontal'">
      <a-row :gutter="24">
        <a-col
          :span="12">
          <a-form-model-item label="设备名称" prop="equipmentName">
            <a-input v-model="form.equipmentName" placeholder="请输入" />
          </a-form-model-item>
        </a-col>
        <a-col
          :span="12">
          <!-- externalBatchNumber -->
          <a-form-model-item label="设备型号" prop="equipmentModel">
            <a-input v-model="form.equipmentModel" placeholder="请输入" />
          </a-form-model-item>
        </a-col>
      </a-row>

      <a-row :gutter="24">
        <a-col
          :span="12">
          <a-form-model-item label="设备编号" prop="equipmentCode">
            <a-input v-model="form.equipmentCode" placeholder="请输入" />
          </a-form-model-item>
        </a-col>

        <a-col :span="12">
          <a-form-model-item label="设备数量" prop="quantityquantity">
            <a-input v-model="form.quantity" placeholder="请输入" />
          </a-form-model-item>
        </a-col>
      </a-row>

      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>

import { addequipment, updateequipment } from '@/api/baseData/device'

export default {
  name: 'CreateForm',
  props: {

  },
  components: {
  },
  data () {
    return {
      submitLoading: false,
      formTitle: '',
      // 表单参数
      form: {
        id: undefined,
        equipmentName: undefined,
        equipmentCode: undefined,
        equipmentModel: undefined,
        quantity: undefined
      },
      open: false,
      rules: {
        quantity: [
          { required: true, message: '数量不能为空', trigger: 'blur' }
        ],
        equipmentCode: [
          { required: true, message: '设备编号不能为空', trigger: 'blur' }
        ],
        equipmentModel: [
          { required: true, message: '设备型号不能为空', trigger: 'blur' }
        ],
        equipmentName: [
          { required: true, message: '设备名称不能为空', trigger: 'blur' }
        ]
      },
      partPlanIncomingList: [],
      inventoryItemList: []
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {

  },
  methods: {

    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {
        receiptNumber: undefined,
        batchNumber: undefined,
        quantity: undefined,
        projectName: undefined,
        planArrivalDate: undefined,
        drawingNumber: undefined,
        partName: undefined,
        remark: undefined,
        handleAddWarehouseReceiptItems: []
      }
    },
     /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.open = true
      this.formTitle = '添加设备库'
      this.form = {
        equipmentName: undefined,
        equipmentCode: undefined,
        equipmentModel: undefined,
        quantity: undefined
      }
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      console.info(row, 'sss')
      this.form = row
      this.formTitle = '修改设备库'
      this.open = true
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined) {
            updateequipment(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addequipment(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
